import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import dynamicRoutes from './dynamic'
// import serviceQuality from './serviceQuality' // 送装服务

/* Router Modules */
// import componentsRouter from './modules/components'
// import chartsRouter from './modules/charts'
// import tableRouter from './modules/table'
// import nestedRouter from './modules/nested'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    noCache: true                if set true, the page will no be cached(default is false)
    affix: true                  if set true, the tag will affix in the tags-view
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * 不需要权限的路由
 */
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: '/index',
    hidden: true,
    name: 'Index',
    children: [{
      path: 'index',
      name: 'HomePage',
      component: () => import('@/views/home/index'),
      meta: { title: '首页', icon: 'el-icon-news', affix: true }
    }]
  },
  {
    path: '/myiframe',
    hidden: true,
    component: Layout,
    redirect: '/myiframe',
    name: 'myiframe',
    children: [{
      path: ':routerPath',
      name: 'iframe',
      component: () => import('@/components/iframe/UrlPath'),
      props: true
    }]
  },
  // 外部系统
  // ...[
  //   {
  //     path: '/call-center/otherOrder',
  //     name: 'otherOrder',
  //     meta: {
  //       hidden: false,
  //       external: true,
  //       title: '其他业务部门服务工单',
  //       icon: 'caidan-tubiaoku'
  //     },
  //     components: {
  //       qup: () => import('@/views/workOrder/otherOrder')
  //     }
  //   },
  //   {
  //     path: '/call-center/otherOrder/details',
  //     name: 'otherOrderDetails',
  //     meta: {
  //       hidden: false,
  //       external: true,
  //       title: '其他业务部门服务工单详情',
  //       icon: 'caidan-tubiaoku'
  //     },
  //     components: {
  //       qup: () => import('@/views/workOrder/otherOrder/details')
  //     }
  //   },
  //   {
  //     path: '/call-center/serviceQuality/orderReportForm',
  //     name: 'callCenterOrderFinishReportForm',
  //     meta: {
  //       hidden: false,
  //       title: '订单完成情况统计表',
  //       icon: 'caidan-tubiaoku'
  //     },
  //     component: () => import('@/views/serviceQuality/orderReportForm')
  //   }
  //   // ...serviceQuality // 送装服务
  // ],
  // FIXME: 动态路由
  ...(() => {
    return /^cc/.test(location.host) || process.env.NODE_ENV === 'development' ? [{
      path: '/',
      name: 'cc',
      component: Layout,
      hidden: true,
      children: dynamicRoutes
    }] : dynamicRoutes.map(e => {
      const meta = e.meta || {}
      meta.external = true
      e.meta = meta
      return e
    })
  })(),
  // -----------
  {
    path: '/taskList',
    name: 'TaskList',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'personTodo',
        component: () => import('@/views/home/schedule'),
        name: 'personTodo',
        meta: { title: '个人待办' }
      }
    ]
  },
  {
    path: '/approve',
    name: 'Approve',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'approveList',
        component: () => import('@/views/home/examine'),
        name: 'ApproveList',
        meta: { title: '待审批' }
      }
    ]
  },
  {
    path: '/messageList',
    name: 'MessageList',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'systemMessage',
        component: () => import('@/views/home/message'),
        name: 'SystemMessage',
        meta: { title: '系统消息' }
      }
    ]
  },
  {
    path: '/setting',
    name: 'Setting',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'userSetting',
        component: () => import('@/views/home/user/Index'),
        name: 'UserSetting',
        meta: { title: '个人中心' }
      }
    ]
  }
]

const createRouter = () => new Router({
  base: `/${process.env.VUE_APP_PORTALCODE}/`,
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
